if not SERVER then return end

if not StarGate.Installed then
	Msg("\n\n\n\n=====================================================\n")
	Msg("ERROR!!!! You need (addons\stargate\lua\stargate\server\ini_parser.lua) of the stargate addon for this spawner to work")
	Msg("\n=====================================================\n\n\n\n")
end

AM_IceFields = {}

function AM_SpawnConfig()

    local map = game.GetMap()
	local ini = INIParser:new("Asteroid_Mining/Asteroidspawner_maps/"..map..".ini")
	
	if (ini) then
	    AM_IceFields = ini.IceField or {}
	    Msg("\n\n\n==========================================================")
		Msg("\n"..tostring(table.ToString(AM_IceFields)).."\n")
		Msg("==========================================================\n\n\n")

		Msg("\n\n\n==========================================================")
		Msg("\n"..tostring(table.ToString(AM_IceFields[1])).."\n")
		Msg("==========================================================\n\n\n")
		
		timer.Simple(1, AM_Spawn_Ice)
		
	end
end
timer.Simple(1, AM_SpawnConfig)

function AM_Spawn_Ice()

    for _,i in pairs(AM_IceFields) do
    	if i.Type and i.Origin then
--===============================================================================================================
-- Custom Shape... Really just a random cluster
--===============================================================================================================
    	    if i.Type == "Custom" and i.Radius then
    	    
    	        local pos = Vector(unpack(i.Origin:TrimExplode(" ")))
    	        local rad = unpack(i.Radius:TrimExplode(" "))
    	        Msg("Pos: " ..tostring(pos).."\n")
    	        Msg("Radius: " ..tostring(rad).."\n")
    	        
    	    
    	        for _,i in pairs(i.BlueIce:TrimExplode(" ")) do
					Msg("Blue Ice: " ..tostring(i).."\n")

					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_blue_ice")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
    	        for _,i in pairs(i.ClearIce:TrimExplode(" ")) do
					Msg("Clear Ice: " ..tostring(i).."\n")
					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_clearice")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
    	        for _,i in pairs(i.GlacialMass:TrimExplode(" ")) do
					Msg("Glacial Mass: " ..tostring(i).."\n")
					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_glacial_mass")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
    	        for _,i in pairs(i.WhiteGlaze:TrimExplode(" ")) do
					Msg("White Glaze: " ..tostring(i).."\n")
					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_white_glaze")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
    	        for _,i in pairs(i.GlareCrust:TrimExplode(" ")) do
					Msg("Glare Crust: " ..tostring(i).."\n")
					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_glare_crust")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
    	        for _,i in pairs(i.DarkGlitter:TrimExplode(" ")) do
					Msg("DarkGlitter: " ..tostring(i).."\n")
					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_dark_glitter")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
    	        for _,i in pairs(i.Gelidus:TrimExplode(" ")) do
					Msg("Gelidus: " ..tostring(i).."\n")
					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_gelidus")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
    	        for _,i in pairs(i.Krystallos:TrimExplode(" ")) do
					Msg("Krystallos: " ..tostring(i).."\n")
					for a=1,i do
					    local randpos = pos + Vector(math.random(-rad, rad),math.random(-rad, rad),math.random(-rad, rad))
						local ent = ents.Create("iceroid_krystallos")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()
					end
    	        end
        		Msg("Frosty!\n")
        	end
--===============================================================================================================
-- The Ring Shape
--===============================================================================================================
        	if i.Type == "Ring" and i.InnerRadius and i.OuterRadius and i.Angle then
        	
        		local pos = Vector(unpack(i.Origin:TrimExplode(" ")))
    	        local inrad = unpack(i.InnerRadius:TrimExplode(" "))
    	        local outrad = unpack(i.OuterRadius:TrimExplode(" "))
    	        local angle = Angle(unpack(i.Angle:TrimExplode(" ")))
    	        Msg("Pos: " ..tostring(pos).."\n")
    	        Msg("Radius: " ..tostring(rad).."\n")


    	        for _,i in pairs(i.BlueIce:TrimExplode(" ")) do
					Msg("Blue Ice: " ..tostring(i).."\n")

					for a=1,i do
      					local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_blue_ice")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
    	        for _,i in pairs(i.ClearIce:TrimExplode(" ")) do
					Msg("Clear Ice: " ..tostring(i).."\n")
					for a=1,i do
      					local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_clearice")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
    	        for _,i in pairs(i.GlacialMass:TrimExplode(" ")) do
					Msg("Glacial Mass: " ..tostring(i).."\n")
					for a=1,i do
      					local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_glacial_mass")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
    	        for _,i in pairs(i.WhiteGlaze:TrimExplode(" ")) do
					Msg("White Glaze: " ..tostring(i).."\n")
					for a=1,i do
         				local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_white_glaze")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
    	        for _,i in pairs(i.GlareCrust:TrimExplode(" ")) do
					Msg("Glare Crust: " ..tostring(i).."\n")
					for a=1,i do
         				local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_glare_crust")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
    	        for _,i in pairs(i.DarkGlitter:TrimExplode(" ")) do
					Msg("DarkGlitter: " ..tostring(i).."\n")
					for a=1,i do
        				 local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_dark_glitter")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
    	        for _,i in pairs(i.Gelidus:TrimExplode(" ")) do
					Msg("Gelidus: " ..tostring(i).."\n")
					for a=1,i do
         				local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_gelidus")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
    	        for _,i in pairs(i.Krystallos:TrimExplode(" ")) do
					Msg("Krystallos: " ..tostring(i).."\n")
					for a=1,i do
        				local X,Y,Z = Calc_Ring(inrad, outrad, angle)
					    local randpos 	= pos + Vector(X, Y, Z)
						local ent = ents.Create("iceroid_krystallos")
							ent:SetPos( randpos )
							ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
							ent.MineralAmount = ent.MineralAmount*2
							ent:Spawn()
							ent:Activate()

					end
    	        end
        	
        	end
        	
		end
		Msg("meh\n\n\n")
	end
end

--This is not totally correct.. but works well enough.. just don't have pitch and roll both 90... or over 45 at the same time
function Calc_Ring(inrad, outrad, angle)
	local rAngle 	= math.random(0,360)
	local rRad  	= math.random(inrad, outrad)

	local zX 		= math.sin(math.rad(angle.p+90))
	local zY 		= math.sin(math.rad(angle.r+90))

	local rX        = (math.sin(math.rad(rAngle))*rRad)*zX
	local rY        = (math.cos(math.rad(rAngle))*rRad)*zY

	local rZx      	= math.sin(math.rad(angle.p))*rRad
	local rZy      	= math.sin(math.rad(angle.r))*rRad

	local normZx     = rZx * (math.sin(math.rad(rAngle)))
	local normZy     = rZy * (math.sin(math.rad(rAngle+90)))
	
	--Msg("rAngle: "..tostring(rAngle).."\n")
	--Msg("rRad: "..tostring(rRad).."\n")
	--Msg("rX: "..tostring(rX).."\n")
	--Msg("rY: "..tostring(rY).."\n")
	--Msg("rZx: "..tostring(rZx).."\n")
	--Msg("rZy: "..tostring(rZy).."\n")
	--Msg("normZx: "..tostring(normZx).."\n")
	--Msg("normZy: "..tostring(normZy).."\n")
	--Msg("Angle.P: "..tostring(angle.p).."\n")
	--Msg("Angle.Y: "..tostring(angle.y).."\n")
	--Msg("Angle.R: "..tostring(angle.r).."\n\n")
	--Msg(tostring(randpos).."\n")
	--Msg("Inner: "..tostring(inrad).."\n")
	--Msg("Outer: "..tostring(outrad).."\n")
	
	return rX, rY, (normZx+normZy)
end

function AM_Spawn_Iceroid(class, pos)
	local ent = ents.Create(class)
		ent:SetPos( pos )
		ent:SetAngles( Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)) )
		ent:Spawn()
		ent:Activate()
end